package com.jdk.demo.reference;

import jdk.internal.ref.Cleaner;

// jdk13的java.base模块未将jdk.internal.ref导出到未命名的模块，从而没能正常加载 jdk.internal.ref.Cleaner类。
// 可以通过配置jdk启动参数解决这个问题，我们修改Tomcat的bin目录下的catalina.sh文件，为JAVA_OPTS增加如下配置：
// JAVA_OPTS="$JAVA_OPTS --add-exports=java.base/jdk.internal.ref=ALL-UNNAMED"
//-Xmx16m
public class CleanerDemo {

	public static void main(String[] args) throws InterruptedException {
		byte[] placeholder = new byte[5 * 1024 * 1024];

		Cleaner cleaner = Cleaner.create(placeholder, new CleanerThread());
		placeholder = null;

		// 触发回收，会把placeholder回收掉
		byte[] placeholder2 = new byte[10 * 1024 * 1024];

		Thread.sleep(5000000);
	}

}

class CleanerThread implements Runnable {

	@Override
	public void run() {
		System.out.println("收回");
	}

}